package com.leinao.survey.service.impl;

import com.leinao.commons.Response;
import com.leinao.constant.ObjectFileTypeEnum;
import com.leinao.project.dao.ProjectMapper;
import com.leinao.project.model.PaiObjectFileRelation;
import com.leinao.survey.dao.UserSurveyMapper;
import com.leinao.survey.model.UserSurvey;
import com.leinao.survey.service.UserSurveyService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @Description 用户反馈调查服务实现类
 * @author lizi
 * @date 2019/06/21 下午6:48
 * @Version 1.0
 */
@Service
public class UserSurveyServiceImpl implements UserSurveyService {
	
	private static Logger logger = LoggerFactory.getLogger(UserSurveyServiceImpl.class);

    @Autowired
    private UserSurveyMapper userSurveyMapper;

    @Autowired
	private ProjectMapper projectMapper;

	/**
	 * 功能描述：添加用户反馈信息
	 * @param userSurvey 用户反馈建议信息
	 * @Author: lizi
	 * @Date: 2019/06/21 下午6:48
	 */
	@Override
	@Transactional
	public Response<UserSurvey> addUserSurvey(UserSurvey userSurvey) {
		long now = System.currentTimeMillis();
		userSurvey.setCreateTime(now);
		userSurvey.setUpdateTime(now);
		userSurveyMapper.insertUserSurvey(userSurvey);

		// 保存用户反馈提交的图片信息
		List<Long> fileIdList = userSurvey.getFileIdList();
		// 封面最多三个，此处采用for循环处理
		if(CollectionUtils.isNotEmpty(fileIdList)) {
			fileIdList.forEach(fileId -> {
				// 保存用户反馈建议图片
				PaiObjectFileRelation pofr = new PaiObjectFileRelation();
				pofr.setFileId(fileId);
				pofr.setType(ObjectFileTypeEnum.FILE_TYPE_USER_SURVEY.getSeq());
				pofr.setObjId(userSurvey.getId());
				pofr.setCreateTime(now);
				pofr.setUpdateTime(now);
				//插入用户反馈建议图片
				projectMapper.insertObjFileRelation(pofr);
			});
		}

		return Response.ok(userSurvey);
	}
}